﻿Imports QLPhuTungOto.Data
Imports QLPhuTungOto.Bus

Public Class ucQLPhieuNhap

    Dim PN_BUS As New PhieuNhapBUS
    Dim CTPN_BUS As New ChiTietPhieuNhapBUS
    Dim fromDay As Date
    Dim toDay As Date

    Dim TrangHienTai As Integer = 1
    Dim TongSoTrang As Integer = 1
    Dim SoDongTrenTrang As Integer = 0

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        Try
            If MessageBox.Show("Bạn thực sự muốn xóa phiếu nhập này ?", "Câu hỏi", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
                PN_BUS.XoaPhieuNhap(Integer.Parse(gvDSPhieuNhap.GetRowCellValue(gvChiTiet.FocusedRowHandle, "MaPhieuNhap").ToString()))
                LoadDSPhieuNhap()
            End If
        Catch ex As Exception
            MessageBox.Show("Không thể xóa phiếu nhập", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
        
    End Sub

    Private Sub ucQLPhieuNhap_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        UcPhanTrang1.txtSoDongCuaTrang.Text = HamHoTro.DocThongSo("config.config", "PhanTrangPhieuNhap", "200")
        SoDongTrenTrang = Integer.Parse(UcPhanTrang1.txtSoDongCuaTrang.Text)
        'LoadDSPhieuNhap()
    End Sub

    Private Sub LoadDSPhieuNhap()
        gcDSPhieuNhap.DataSource = PN_BUS.LayDSPhieuNhap()
    End Sub

    Private Sub LoadCTPNTheoMaPN()
        Try
            gcChiTiet.DataSource = CTPN_BUS.LayCTPNTheoMaPN(Integer.Parse(gvDSPhieuNhap.GetRowCellValue(gvDSPhieuNhap.FocusedRowHandle, "MaPhieuNhap")))
        Catch ex As Exception
            gcChiTiet.DataSource = Nothing
        End Try
    End Sub

    Private Sub gvDSPhieuNhap_FocusedRowChanged(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs) Handles gvDSPhieuNhap.FocusedRowChanged

        Try
            Dim selectedIndex As Integer = 0
            selectedIndex = gvDSPhieuNhap.FocusedRowHandle

            lblNgayNhap.Text = gvDSPhieuNhap.GetRowCellValue(selectedIndex, "NgayNhap")
            lblTongGiaTri.Text = gvDSPhieuNhap.GetRowCellValue(selectedIndex, "TongTien")

            LoadCTPNTheoMaPN()
        Catch ex As Exception

        End Try
        
    End Sub

    Private Sub dtTuNgay_EditDateModified(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtTuNgay.EditDateModified
        Try
            fromDay = dtTuNgay.DateTime
        Catch ex As Exception

        End Try

    End Sub

    Private Sub dtDenNgay_EditDateModified(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtDenNgay.EditDateModified
        Try
            toDay = dtDenNgay.DateTime
        Catch ex As Exception

        End Try

    End Sub

    Private Sub btnHienThi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHienThi.Click

        Try
            TinhSoTrang()
            LoadDSPNTheoThoiGian()
        Catch ex As Exception

        End Try

    End Sub

    Private Sub LoadDSPNTheoThoiGian()
        SoDongTrenTrang = Integer.Parse(Integer.Parse(UcPhanTrang1.txtSoDongCuaTrang.Text))
        gcDSPhieuNhap.DataSource = PN_BUS.LayDSPNTheoThoiGian(dtTuNgay.DateTime, dtDenNgay.DateTime, (TrangHienTai - 1) * SoDongTrenTrang, SoDongTrenTrang)
        UcPhanTrang1.lblTrangHienTai.Text = TrangHienTai.ToString + "/" + TongSoTrang.ToString
    End Sub

    Private Sub CapNhatStatus()
        ToolStripStatusLabel2.Text = dtTuNgay.DateTime.ToShortDateString()
        ToolStripStatusLabel4.Text = dtDenNgay.DateTime.ToShortDateString() + ": "
        ToolStripStatusLabel5.Text = PN_BUS.DemPNTheoThoiGian(fromDay, toDay).ToString()
    End Sub
    Private Sub gvDSPhieuNhap_DataSourceChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles gvDSPhieuNhap.DataSourceChanged
        CapNhatStatus()
    End Sub

    Private Sub ucQLPhieuNhap_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        
        'LoadDSPhieuNhap()
    End Sub

    Private Sub UcPhanTrang1_SuaSoDongTrenTrang() Handles UcPhanTrang1.SuaSoDongTrenTrang
        Try
            SoDongTrenTrang = Integer.Parse(UcPhanTrang1.txtSoDongCuaTrang.Text)

            If SoDongTrenTrang > 0 Then
                TinhSoTrang()
                TrangHienTai = 1
                LoadDSPNTheoThoiGian()
                HamHoTro.GhiThongSo("config.config", "PhanTrangPhieuNhap", UcPhanTrang1.txtSoDongCuaTrang.Text.Trim)
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub UcPhanTrang1_TrangCuoi_Click() Handles UcPhanTrang1.TrangCuoi_Click
        Try
            TrangHienTai = TongSoTrang
            LoadDSPNTheoThoiGian()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub UcPhanTrang1_TrangDau_Click() Handles UcPhanTrang1.TrangDau_Click
        Try
            TrangHienTai = 1
            LoadDSPNTheoThoiGian()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub UcPhanTrang1_TrangSau_Click() Handles UcPhanTrang1.TrangSau_Click
        Try
            If TrangHienTai < TongSoTrang Then
                TrangHienTai += 1
                LoadDSPNTheoThoiGian()
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub UcPhanTrang1_TrangTruoc_Click() Handles UcPhanTrang1.TrangTruoc_Click
        Try
            If TrangHienTai > 1 Then
                TrangHienTai -= 1
                LoadDSPNTheoThoiGian()
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Function TinhSoTrang() As Integer
        Dim st As Integer = 0
        Try

            Dim soluongpn As Integer = PN_BUS.DemPNTheoThoiGian(fromDay, toDay)
            st = (soluongpn \ SoDongTrenTrang)

            If soluongpn Mod SoDongTrenTrang <> 0 Then
                st += 1
            End If

            TongSoTrang = st

        Catch ex As Exception

        End Try
        Return st
    End Function

    Private Sub LabelControl1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles LabelControl1.Paint
        Try
            'TinhSoTrang()
            'LoadDSPNTheoThoiGian()
        Catch ex As Exception

        End Try
    End Sub
End Class
